package com.zeta.file.controller;

import com.zeta.file.model.entity.FsFile;
import com.zeta.file.model.query.FsFileQueryDTO;
import com.zeta.file.service.IFsFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.zetaframework.annotation.log.SysLog;
import org.zetaframework.annotation.satoken.PreAuth;
import org.zetaframework.annotation.satoken.PreCheckPermission;
import org.zetaframework.annotation.satoken.PreMode;
import org.zetaframework.base.result.ApiResult;
import org.zetaframework.controller.SuperSimpleController;
import org.zetaframework.controller.curd.DeleteController;
import org.zetaframework.controller.curd.QueryController;

import java.util.Set;

/**
 * 文件管理 前端控制器
 *
 * @author gcc
 */
@Tag(name = "文件管理", description = "1.文件管理")
@PreAuth(replace = "fs:file")
@RestController
@RequestMapping("/fs/file")
public class FsFileController extends SuperSimpleController<IFsFileService, FsFile>
    implements QueryController<FsFile, Long, FsFileQueryDTO>,
        DeleteController<FsFile, Long>
{

    /**
     * 上传文件
     * <p>
     * 参考：
     * <a href="https://springdoc.org/#is-file-upload-supported">Is file upload supported ?</a>
     *
     * @param file 文件对象
     * @param bizType 业务类型 例如：order、user_avatar等 会影响文件url的值
     */
    @SysLog(request = false)
    @PreCheckPermission(value = {"{}:add", "{}:save"}, mode = PreMode.OR)
    @Operation(summary = "上传文件")
    @PostMapping(value = "/upload", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public ApiResult<FsFile> upload(
            @Parameter(description = "选择要上传的文件")
            @RequestPart("file")
            final MultipartFile file,
            @RequestParam(required = false)
            @Parameter(description = "业务类型 例如：order、user_avatar等 会影响文件url的值", example = "avatar")
            String bizType
    ) {
        return success(service.upload(file, bizType));
    }

    /**
     * 下载文件
     *
     * @param id 文件id
     * @param response HttpServletResponse
     */
    @SysLog(response = false)
    @PreCheckPermission(value = "{}:export")
    @Operation(summary = "下载文件")
    @GetMapping(value = "/download/{id}", produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE})
    public void download(
            @PathVariable("id") @Parameter(description = "文件id") Long id,
            HttpServletResponse response
    ) {
        service.download(id, response);
    }

    /**
     * 自定义单体删除
     *
     * @param id Id
     * @return ApiResult<Boolean>
     */
    @Override
    public ApiResult<Boolean> handlerDelete(Long id) {
        return success(service.delete(id));
    }

    /**
     * 自定义批量删除
     *
     * @param ids Id
     * @return ApiResult<Boolean>
     */
    @Override
    public ApiResult<Boolean> handlerBatchDelete(Set<Long> ids) {
        return success(service.batchDelete(ids));
    }

}
